﻿using Fishing.Abstractions;
using Fishing.Common;
using Fishing.Converter;
using System;
using System.Text.Json.Serialization;
using System.Windows.Input;

namespace Fishing.Model
{
    [JsonConverter(typeof(SettingsJsonConverter))]
    public class Settings : ISettings
    {
        public bool Startup { get; set; }
        public bool TaskbarIconVisible { get; set; }
        public IncreaseMode IncreaseMode { get; set; }
        public IncreaseAlgorithm IncreaseAlgorithm { get; set; }

        private KeyGesture _quickUpdateKeyBinding;
        public KeyGesture QuickUpdateKeyBinding
        {
            get => _quickUpdateKeyBinding;
            set
            {
                _quickUpdateKeyBinding = value;
                KeyBindingChanged?.Invoke(null, EventArgs.Empty);
            }
        }

        private KeyGesture _forceQuitKeyBinding;
        public KeyGesture ForceQuitKeyBinding
        {
            get => _forceQuitKeyBinding;
            set
            {
                _forceQuitKeyBinding = value;
                KeyBindingChanged?.Invoke(null, EventArgs.Empty);
            }
        }

        private KeyGesture _exhaleKeyBinding;
        public KeyGesture ExhaleKeyBinding
        {
            get => _exhaleKeyBinding;
            set
            {
                _exhaleKeyBinding = value;
                KeyBindingChanged?.Invoke(null, EventArgs.Empty);
            }
        }

        public bool TrySetKeyBingding(KeyBindingMap map, KeyGesture keyGesture)
        {
            switch (map)
            {
                case KeyBindingMap.QuickUpdate:
                    {
                        return !((keyGesture.Key == _forceQuitKeyBinding.Key && keyGesture.Modifiers == _forceQuitKeyBinding.Modifiers) ||
                            (keyGesture.Key == _exhaleKeyBinding.Key && keyGesture.Modifiers == _exhaleKeyBinding.Modifiers));
                    }
                case KeyBindingMap.ForceQuit:
                    {
                        return !((keyGesture.Key == _quickUpdateKeyBinding.Key && keyGesture.Modifiers == _quickUpdateKeyBinding.Modifiers) ||
                            (keyGesture.Key == _exhaleKeyBinding.Key && keyGesture.Modifiers == _exhaleKeyBinding.Modifiers));
                    }
                case KeyBindingMap.Exhale:
                    {
                        return !((keyGesture.Key == _quickUpdateKeyBinding.Key && keyGesture.Modifiers == _quickUpdateKeyBinding.Modifiers) ||
                            (keyGesture.Key == _forceQuitKeyBinding.Key && keyGesture.Modifiers == _forceQuitKeyBinding.Modifiers));
                    }
            }

            return false;
        }

        public event EventHandler KeyBindingChanged;

        public Settings()
        {
            Startup = false;
            TaskbarIconVisible = true;
            IncreaseMode = IncreaseMode.Random;
            IncreaseAlgorithm = IncreaseAlgorithm.Linear;
            QuickUpdateKeyBinding = new KeyGesture(Key.R, ModifierKeys.Control);
            ForceQuitKeyBinding = new KeyGesture(Key.E, ModifierKeys.Control);
            ExhaleKeyBinding = new KeyGesture(Key.Q, ModifierKeys.Control);
        }
    }
}